import Vue from 'vue'
import Router from 'vue-router'
import Login from "../pages/Login";
import Register from "../pages/Register";
import myOrder from "../components/Home/myOrder";
import FrontPage from "../pages/FrontPage";
import Nav from "../components/Home/Nav";
import Product from "../components/Home/Product";
import Error from "../components/Error";
import Backstage from "../pages/Backstage";
import UserModule from "../components/backChildren/UserModule";
import WheelchairModule from "../components/backChildren/WheelchairModule";
import RentalInformation from "../components/backChildren/RentalInformation";
import InformationStatistics from "../components/backChildren/InformationStatistics";
import HospitalModule from "../components/backChildren/HospitalModule";
import Contact from "../components/Home/Contact";
import Test from "../components/Test";
Vue.use(Router)

const router =  new Router({
  routes: [
    {path:'/test',component:Test},
    {
      path:'*',
      component:Error,
      meta:{title:'错误'}
    },
    {
      path:'/',
      component: Login,
      meta:{title:'共享轮椅登录页'}
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta:{title:'共享轮椅登录页'}
    },
    {
      path:'/register',
      name:'register',
      component:Register,
      meta:{title:'共享轮椅注册页'}
    },
    {
      path:'/home',
      name:'home',
      redirect:'/home/nav',
      component:FrontPage,
      meta:{title:'共享轮椅',user:'用户'},
      children:[
        {
          path:'nav',
          component:Nav,
          meta:{title:'共享轮椅首页',user:'用户'},
        },
        {
          path:'product',
          component:Product,
          meta:{title:'共享轮椅产品',user:'用户'},
        },
        {
          path:'order',
          name:'myOrder',
          component:myOrder,
          meta:{title:'我的订单',user:'用户'}
        },
        {
          path: 'contact',
          component: Contact,
          meta:{title:'联系我们',user:'用户'}
        }
      ]
    },
    {
      path:'/backstage',
      name:'backstage',
      component:Backstage,
      redirect:'/backstage/user',
      meta:{title:'共享轮椅后台管理系统',user:'管理员'},
      children:[
        {
          path: 'user',
          component:UserModule,
          meta:{title:'共享轮椅后台管理系统',user:'管理员'},
        },
        {
          path: 'wheelchair',
          component:WheelchairModule,
          meta:{title:'共享轮椅后台管理系统',user:'管理员'},
        },
        {
          path: 'rent',
          component:RentalInformation,
          meta:{title:'共享轮椅后台管理系统',user:'管理员'},
        },
        {
          path: 'statistics',
          component: InformationStatistics,
          meta:{title:'共享轮椅后台管理系统',user:'管理员'},
        },
        {
          path: 'hospital',
          component: HospitalModule,
          meta:{title:'共享轮椅后台管理系统',user:'管理员'},
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next)=>{
  let adminId = localStorage.getItem('ADMINTOKEN')
  let userId = localStorage.getItem('USERTOKEN')
  if ((to.path === '/' || to.path === '/login') && (adminId !== null || userId !== null)){
    next('/backstage')
  }else if (userId !== null && to.meta.user === '管理员'){
    next('/home')
  }else if (adminId !==null && to.meta.user === '用户'){
    next('/backstage')
  }else if (adminId === null && to.meta.user === '管理员'){
    next('/')
  }else if (userId === null && to.path === '/home/order'){
    next('/')
  }else {
    next()
  }
})

router.afterEach((to, from)=>{
  document.title = to.meta.title
})

export default router
